package com.ucenter.server.org.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ucenter.server.org.dao.OrgInfoDao;
import com.ucenter.server.org.pojo.OrgInfo;
import com.ucenter.server.org.service.OrgInfoService;
import com.system.comm.model.KvEntity;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseFrame;
import com.system.handle.model.ResponseCode;

/**
 * org_info的Service
 * @author autoCode
 * @date 2018-12-01 09:50:14
 * @version V1.0.0
 */
@Component
public class OrgInfoServiceImpl implements OrgInfoService {

	@Autowired
	private OrgInfoDao orgInfoDao;
	
	@Override
	public ResponseFrame saveOrUpdate(OrgInfo orgInfo) {
		ResponseFrame frame = new ResponseFrame();
		if(FrameStringUtil.isEmpty(orgInfo.getOrgId())) {
			orgInfo.setOrgId(FrameNoUtil.uuid());
			orgInfoDao.save(orgInfo);
		} else {
			orgInfoDao.update(orgInfo);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public OrgInfo get(String orgId) {
		return orgInfoDao.get(orgId);
	}

	@Override
	public ResponseFrame pageQuery(OrgInfo orgInfo) {
		orgInfo.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = orgInfoDao.findOrgInfoCount(orgInfo);
		List<OrgInfo> data = null;
		if(total > 0) {
			data = orgInfoDao.findOrgInfo(orgInfo);
		}
		Page<OrgInfo> page = new Page<OrgInfo>(orgInfo.getPage(), orgInfo.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String orgId) {
		ResponseFrame frame = new ResponseFrame();
		orgInfoDao.delete(orgId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<OrgInfo> findAll() {
		return orgInfoDao.findAll();
	}

	@Override
	public List<KvEntity> findKvAll() {
		List<KvEntity> data = new ArrayList<KvEntity>();
		List<OrgInfo> list = findAll();
		for (OrgInfo org : list) {
			data.add(new KvEntity(org.getOrgId(), org.getName()));
		}
		return data;
	}
}